package com.canbot.u05.activity.agenda

import android.app.Activity
import android.content.Intent
import android.support.v7.widget.GridLayoutManager
import android.view.View
import com.canbot.u05.R
import com.canbot.u05.activity.agenda.adapter.AgendaPosAdapter
import com.canbot.u05.activity.agenda.bean.AgendaPosBean
import com.canbot.u05.activity.agenda.interfaces.IAgendaTypeLisenter
import com.canbot.u05.activity.presidemode.newcode.BaseHostActivity
import com.canbot.u05.activity.presidemode.newcode.bean.NetLoadUtil
import com.canbot.u05.activity.presidemode.newcode.interfaces.IResponse
import com.canbot.u05.bean.guide.MapInfo
import com.canbot.u05.utils.PathConst
import kotlinx.android.synthetic.main.activity_agenda_work_pos.*
import java.util.*

/**
 * @author yinxiaowei
 * @date 2019/5/31
 */
class AgendaWorkPosActivity : BaseHostActivity() {
    private val voices = arrayOf(arrayOf("${PathConst.AGENDA_VOICE_DIR}gzdd_gzrc_u05_001.mp3", "32"),
            arrayOf("${PathConst.AGENDA_VOICE_DIR}gzdd_gzrc_u05_002.mp3", "38"))
    private var mapInfo: MapInfo? = null
    private lateinit var adapter: AgendaPosAdapter
    private var mAgendaPosBean: AgendaPosBean? = null
    override fun initEventData() {
        loaddata()
        initIntent()
        playRandomVoiceWithEmotion(voices)
    }

    private fun initIntent() {
        mAgendaPosBean = intent.getSerializableExtra(DATA) as AgendaPosBean?
    }

    private fun mockData() {

    }

    private fun loaddata() {
        showLoadingDialog(getString(R.string.load_hint))
        NetLoadUtil.getMapInfo(this, object : IResponse<MapInfo> {
            override fun onSucusse(result: MapInfo?) {
                dismissDialog()
                mapInfo = result
                showListView()
            }

            override fun onFailuer(result: String) {
                dismissDialog()
            }
        })
    }

    private fun showListView() {
        runOnUiThread {
            titlePos.visibility = View.VISIBLE
            empty.visibility = View.INVISIBLE
            recycleView.visibility = View.VISIBLE
            initRecycleView()
        }
    }

    private fun showEmptyView() {
        runOnUiThread {
            empty.visibility = View.VISIBLE
            recycleView.visibility = View.INVISIBLE
        }
    }

    private fun initRecycleView() {
        val localPoints = mapInfo?.localPoints
        val agendaPosBeanList = ArrayList<AgendaPosBean>()
        localPoints?.forEach {
            agendaPosBeanList.add(agendaPosBean(it.pointName))
        }
        agendaPosBeanList.add(0, agendaPosBean(getString(R.string.current_post)))
        recycleView.layoutManager = GridLayoutManager(this, 3)
        adapter = AgendaPosAdapter(agendaPosBeanList, this, object : IAgendaTypeLisenter {
            override fun onSelect(pos: Int) {
                agendaPosBeanList.forEach {
                    it.isSelect = false
                }
                agendaPosBeanList[pos].isSelect = true
                mAgendaPosBean = agendaPosBeanList[pos]
                adapter.notifyDataSetChanged()
                exit()
            }
        })
        recycleView.adapter = adapter
    }

    private fun agendaPosBean(name: String): AgendaPosBean {
        val element = AgendaPosBean()
        element.name = name
        element.isSelect = mAgendaPosBean?.name == element.name
        return element
    }

    override fun getLayoutResId(): Int {
        return R.layout.activity_agenda_work_pos
    }

    fun back(view: View) {
        exit()
    }

    private fun exit() {
        val intent = Intent()
        intent.putExtra(BaseHostActivity.DATA, mAgendaPosBean)
        setResult(Activity.RESULT_OK, intent)
        finish()
    }

    override fun onBackPressed() {
        exit()
    }
}
